import * as path from 'path';
import * as fs from 'fs';
import {
    PolyvVideoDownloader
} from "./PolyvVideoDownloader";
import {
    FileDownloader
} from "./FileDownloader";

const POLYV_VIDEO = "PolyvVideo";
const BOS_VIDEO = "BosVideo";
const USER_SELF_PPT = "selfPpt"
const USER_SELF_PDF = "selfPdf"
const USER_SELF_WORD = "selfWord"
const USER_SELF_IMG = "selfImg"
const USER_SELF_VIDEO = "selfVideo"
const USER_SELF_AUDIO = "selfAudio"
const COURSEWARE = "courseware"

export class SingleDownloadTask {

    /**
     * 单个业务类型文件下载器（处理多种类型的文件）
     * @param downloadParam
     */
    constructor(downloadParam, successCallback) {
        // debugger
        // console.log(downloadParam);
        // return
        if (downloadParam.FileName) {
            var lastIndexOf = downloadParam.FileName.lastIndexOf('\\')
        }
        // console.log('lastIndexOf=>>>>', lastIndexOf)
        if (lastIndexOf && lastIndexOf > 0) {
            downloadParam.FileName = downloadParam.FileName.slice(lastIndexOf + 1)
        }
        this.downloaderId = downloadParam.TaskId;
        this.downloadParam = downloadParam;
        let _this = this;
        //保利威视
        // console.log('downloadParam--------------->' , downloadParam);
        if (downloadParam.FileType === POLYV_VIDEO) {
            this.fileName = path.join(window.$DATADIR, "resc", downloadParam.FileType, downloadParam.FileName);
            if (!fs.existsSync(this.fileName)) {
                fs.mkdirSync(this.fileName);
            }
            downloadParam.FileName = this.fileName;
            if (!downloadParam.Vid) {
                let bsrContent = downloadParam.Bsr;
                bsrContent = JSON.parse(bsrContent);
                downloadParam.Vid = bsrContent["vid"];
            }
            let polyVideo = new PolyvVideoDownloader(downloadParam, function () {
                _this.fileDownloader = polyVideo;
                if (successCallback) {
                    successCallback();
                }
            });
        } else {
            if (downloadParam.FileType === 'MindMap') {
                const bsr = JSON.parse(downloadParam.Bsr)
                if (bsr && bsr.resourceJson) {
                    const mindMapJson = JSON.parse(bsr.resourceJson)[0].name
                    let tempDir = path.join(window.$DATADIR, "resc", "MindMap", downloadParam.TaskId);
                    if (!fs.existsSync(tempDir)) {
                        fs.mkdirSync(tempDir)
                    }
                    fs.writeFileSync(path.join(window.$DATADIR, "resc", "MindMap", downloadParam.TaskId + '_b_s_c_' + mindMapJson + '_' + Date.now() + '.json'), bsr.resourceJson)
                }
            }
            if (downloadParam.FileType === BOS_VIDEO) {
                // const bsr = JSON.parse(downloadParam.Bsr)
                // console.log('新视频------------------------------',bsr);
                // let tempDir = path.join(window.$DATADIR, "resc", downloadParam.FileType, downloadParam.TaskId,);
                // console.log('tempDir----------->' + tempDir);
                // if (!fs.existsSync(tempDir)) {
                //     fs.mkdirSync(tempDir)
                // }
            }
            //用户上传的ppt
            if (downloadParam.FileType === USER_SELF_PPT) {
                console.log('downloadParam',downloadParam);
                if (downloadParam.TaskId) {
                    let taskId = downloadParam.TaskId
                    let tempDir = path.join(window.$DATADIR, "resc", "user", "ppt", taskId);
                    if (!fs.existsSync(tempDir)) {
                        fs.mkdirSync(tempDir);
                    }
                    downloadParam.FileName = path.join(window.$DATADIR, "resc", "user", "ppt", taskId, downloadParam.FileName);
                }
            }else if(downloadParam.FileType === USER_SELF_PDF){
                if (downloadParam.TaskId) {
                    let taskId = downloadParam.TaskId
                    let tempDir = path.join(window.$DATADIR, "resc", "user", "pdf", taskId);
                    if (!fs.existsSync(tempDir)) {
                        fs.mkdirSync(tempDir);
                    }
                    downloadParam.FileName = path.join(window.$DATADIR, "resc", "user", "pdf", taskId, downloadParam.FileName);
                }
            }else if(downloadParam.FileType === USER_SELF_WORD){
                if (downloadParam.TaskId) {
                    let taskId = downloadParam.TaskId
                    let tempDir = path.join(window.$DATADIR, "resc", "user", "word", taskId);
                    if (!fs.existsSync(tempDir)) {
                        console.log('mkdirSync',tempDir);
                        fs.mkdirSync(tempDir);
                    }
                    downloadParam.FileName = path.join(window.$DATADIR, "resc", "user", "word", taskId, downloadParam.FileName);
                }
            }else if(downloadParam.FileType === USER_SELF_IMG){
                if (downloadParam.TaskId) {
                    let taskId = downloadParam.TaskId
                    let tempDir = path.join(window.$DATADIR, "resc", "user", "img", taskId);
                    if (!fs.existsSync(tempDir)) {
                        fs.mkdirSync(tempDir);
                    }
                    downloadParam.FileName = path.join(window.$DATADIR, "resc", "user", "img", taskId, downloadParam.FileName);
                }
            }else if(downloadParam.FileType === USER_SELF_VIDEO){
                if (downloadParam.TaskId) {
                    let taskId = downloadParam.TaskId
                    let tempDir = path.join(window.$DATADIR, "resc", "user", "video", taskId);
                    if (!fs.existsSync(tempDir)) {
                        fs.mkdirSync(tempDir);
                    }
                    downloadParam.FileName = path.join(window.$DATADIR, "resc", "user", "video", taskId, downloadParam.FileName);
                }
            }else if(downloadParam.FileType === USER_SELF_AUDIO){
                if (downloadParam.TaskId) {
                    let taskId = downloadParam.TaskId
                    let tempDir = path.join(window.$DATADIR, "resc", "user", "audio", taskId);
                    if (!fs.existsSync(tempDir)) {
                        fs.mkdirSync(tempDir);
                    }
                    downloadParam.FileName = path.join(window.$DATADIR, "resc", "user", "audio", taskId, downloadParam.FileName);
                }
            }else {
                downloadParam.FileName = path.join(window.$DATADIR, "resc", downloadParam.FileType, downloadParam.FileName);
            }
            this.fileDownloader = new FileDownloader(downloadParam.Url, downloadParam.FileName);
            console.log(downloadParam.FileType+' 资源下载地址：',downloadParam.Url);
            FileDownloader.getTotalSize(downloadParam.Url, resSize => {
                // console.log(downloadParam.Url);
                // return
                _this.fileDownloader.totalSize = resSize;
                if (successCallback) {
                    console.log('');
                    successCallback();
                }
            });
        }
    }
}
